package broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.widget.Toast;

import mytools.NetWorkUtil;
import ui.LoginActivity;

public class UpdateReceiver extends BroadcastReceiver {

    private Context mContext;

    public UpdateReceiver(Context context) {
        mContext = context;
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast toast;
        int code = intent.getIntExtra("updatecode", -1);
        if (code != 0x17) {//网络连接正常
            ((LoginActivity) mContext).setNetworkAvailable(true);
//            ((LoginActivity) mContext).doGetSBXH(((LoginActivity) mContext).getMyApplication().getInfos().get("SERIAL"), NetWorkUtil.MAC_ADD);
        }
        switch (code) {
            case 0x11://系统版本最新
                toast = Toast.makeText(mContext, "应用已是最新版本！", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                break;
            case 0x12://检测到新版本
                int newVerCode = intent.getIntExtra("newVerCode", -1);
                String newVerName = intent.getStringExtra("newVerName");
                ((LoginActivity) mContext).showUpdateDialog(newVerCode, newVerName);
                break;
            case 0x14://下载文件不存在
                ((LoginActivity) mContext).cancelProgressDialog();
                toast = Toast.makeText(mContext, "下载文件不存在！", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                break;
            case 0x15://下载文件超时
                ((LoginActivity) mContext).cancelProgressDialog();
                toast = Toast.makeText(mContext, "下载超时！", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                break;
            case 0x17://网络未连接
                toast = Toast.makeText(mContext, "网络未连接！", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                break;
            case 0x18://无法连接到服务器
                toast = Toast.makeText(mContext, "获取更新数据失败！", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                break;
        }
    }
}